home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 13830 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.2 KB

  1. Path: erinews.ericsson.se!usenet
  2. From: etmbgom@ericsson.se (Ben van Gompel)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Reverting of big array ?
  5. Date: 27 Mar 1996 15:54:59 GMT
  6. Organization: Ericsson Telecommunicatie BV.
  7. Message-ID: <4jbock$s7j@erinews.ericsson.se>
  8. References: <31587ACF.59D9@teleport.com>
  9. Reply-To: etmbgom@ericsson.se
  10. NNTP-Posting-Host: worf.etm.ericsson.se
  11.  
  12. In article <31587ACF.59D9@teleport.com>, GHouck <hksys@teleport.com> writes:
  13. > Thomas wrote:
  14. > > I need to revert an array (of chars).
  15. > > I used the folowing code:( l=length of array)
  16. > > 
  17. > >   char *rev=new char[l+1];
  18. > >   for(i=l-1,j=0;i>=0;i--,j++)  rev[j]=seq[i];
  19. > >   rev[j]='\0';
  20. > > 
  21. > > this code works, but is terribly slow, when using an string with
  22. > > size ~ 6000.
  23. > > doing the same with the unix command 'rev' takes 1/2 second.
  24. > > Any ideas  ???Thomas,
  25. > It might not be much faster, but I usually use the following:
  26. >   for( i=0; i<numChars/2; i++ )
  27. >     strChars[i] = strChars[numChars-i-1];
  28.  
  29. That won't work.
  30. You better use something like:
  31.  
  32.   char* newChars = new char[numChars+1]
  33.   for( i=0; i<numChars; i++ )
  34.   {
  35.      newChars[i] = oldChars[numChars-i-1];
  36.   }
  37.   newChars[numChars] = '\0';
  38.  
  39. But still, this won't be fast enough I'd guess.
  40.  
  41. Regards, Ben
  42.  
  43.